<?

include 'Lib.php';

class Place
{
    public $id = 0;
    public $name;
    public $description;
    public $address;
    public $direction;
    public $featured;
    public $tag;
    public $lat;
    public $lon;
    //public $scoreTotal;
    //public $scoreNumberPeople;
    
    public $Exists;
        
    
    public function Place($placeID=NULL)
    {
        if($placeID == NULL)
            return;
        
        $this->id = $placeID;
        $this->FillMe($placeID);       
    }
    
    public static function FillObject($row,$place)
    {        
        $place->id = $row["ID"];
        $place->name = $row["Name"];
        $place->description = $row["Description"];
        $place->address = $row["Address"];
        $place->direction = $row["Direction"];
        $place->featured = $row["Featured"];        
        $place->tag = $row["Tag"];
        $place->lat = $row["Lat"];
        $place->lon = $row["Lon"];
        //$place->scoreTotal = $row["ScoreTotal"];
        //$place->scoreNumberPeople = $row["ScoreNumberPeople"];
        
        return $place;
    }
    
    private function FillMe($id)
    {        
        $result = mysql_query("SELECT * FROM PLACE WHERE ID=$id");
        
        if($result == null)
        {
            $Exists = false;
            return;
        }
        
        $row = mysql_fetch_array($result);
        
        Place::FillObject($row,$this);
        
        $Exists = true;
    }
    
    public function Save()
    {
        //echo($this->name);
        
        if($this->id == 0)
        {
            $sql = "INSERT INTO PLACE (NAME,DESCRIPTION,ADDRESS,DIRECTION,FEATURED,TAG,LAT,LON) VALUES ('$this->name', '$this->description','$this->address','$this->direction', '$this->featured', '$this->tag', '$this->lat', '$this->lon' )";
        }
        else
        {
            $sql = "UPDATE PLACE SET NAME='$this->name', DESCRIPTION='$this->description', ADDRESS='$this->address', DIRECTION='$this->direction', FEATURED='$this->featured', TAG='$this->tag', LAT=$this->lat, LON=$this->lon WHERE ID=$this->id";            
        }
        //echo($sql);
        mysql_query($sql) or die("Can't Save the Place Object");       
    }
    
    public function GetPhotos()
    {
        $result = mysql_query("SELECT * FROM MEDIA WHERE PLACE_ID=$this->id AND MEDIATYPE=0") or die("Can't get the photos of this place");
                
        $result_array = array();
        
        $key = 0;
        
        while($row = mysql_fetch_array($result))
        {
            $photo = new Media();
            
            $photo->id = $row["ID"];
            $photo->fileName = $row["Name"];
         
            $result_array[$key++] = $photo;            
        }
         
        return $result_array;
    }
    
    public function GetMainPhoto()
    {
        $result = mysql_query("SELECT * FROM MEDIA WHERE PLACE_ID=$this->id AND MEDIATYPE=0 AND IsDefault=1") or die("Can't get the photos of this place");
                
        $result_array = array();
        
        $key = 0;

        $photo = new Media();

        while($row = mysql_fetch_array($result))
        {
            $photo->id = $row["ID"];
            $photo->fileName = $row["Name"];
            
            return $photo;
        }
         
        $photo->id = 0;
        $photo->fileName = "imagenotexist.png";
         
        return $photo;    
    }
    
    public function GetLogo()
    {
        $result = mysql_query("SELECT * FROM MEDIA WHERE PLACE_ID=$this->id AND MEDIATYPE=1") or die("Can't get the logo of this place");
                
        $result_array = array();
        
        $key = 0;

        $photo = new Media();

        while($row = mysql_fetch_array($result))
        {
            $photo->id = $row["ID"];
            $photo->fileName = $row["Name"];
            
            return $photo;
        }
         
        $photo->id = 0;
        $photo->fileName = "imagenotexist.png";
         
        return $photo;  
    }
    
    public function GetRate()
    {
        $result = mysql_query("SELECT * FROM RATING WHERE Place_ID=$this->id");
        
        $result_array = array();
        
        $key = 0;
        $totalPoints = 0;
        
        while($row = mysql_fetch_array($result)){
            
            //$place = new Place();
            //Place::FillObject($row,$place);
            
            //echo("Done:" . $place->name . "<br>");
            $key++;
            $totalPoints+= $row["Points"];            
        }
        
        if($key > 0)
            return $totalPoints / $key;
                
        return 0;
    }
    
    public function Rate($userID, $points)
    {
        //$this->scoreNumberPeople++;
        //$this->scoreTotal+=$points;
        
        //$sql = "UPDATE PLACE SET SCORENUMBERPEOPLE=$this->scoreNumberPeople, SCORETOTAL=$this->scoreTotal WHERE ID=$this->id";
        
        $sql = "DELETE FROM RATING WHERE Place_ID=$this->id AND Member_ID=$userID";                
        mysql_query($sql) or die("Can't delete the user ratings");

        $sql = "INSERT INTO RATING (Place_ID, Member_ID, Points) VALUES ($this->id, $userID, $points)";                
        mysql_query($sql) or die("Can't add the user ratings");        
    }
    
    public function AddPhoto($photoFileName,$description="", $isDefault=0)
    {
        $sql = "INSERT INTO MEDIA (Name, Description, IsDefault, Place_ID) VALUES ('$photoFileName', '$description', '$isDefault', $this->id)";
        
        mysql_query($sql) or die("Can't add photo");
    }

    public function AddLogo($photoFileName,$description="", $isDefault=0)
    {
        // MEDIA_TYPE = 1

        $sql = "DELETE FROM MEDIA WHERE Place_ID=$this->id AND MEDIATYPE=1";   // Delete all the logos for this place first     
        mysql_query($sql) or die("Can't delete place logo");
        //echo($sql);
        $sql = "INSERT INTO MEDIA (Name, Description, IsDefault, Place_ID, MEDIATYPE) VALUES ('$photoFileName', '$description', '$isDefault', $this->id, 1 )";        
        mysql_query($sql) or die("Can't add place logo");
    }    
    
    public static function Delete($placeID)
    {
        mysql_query("DELETE FROM PLACE WHERE ID=$placeID") or die("Can't delete the Place");
    }
    
    public static function GetPlaces()
    {
        $result = mysql_query("SELECT * FROM PLACE");
        
        $result_array = array();
        
        $key = 0;
        
        while($row = mysql_fetch_array($result)){
            
            $place = new Place();
            Place::FillObject($row,$place);
            
            //echo("Done:" . $place->name . "<br>");
            
            $result_array[$key++] = $place;
            
        }
         
        //echo($result_array[0]->name);
        
        return $result_array;
    }
    
}

class Media
{
    public $id = 0;
    public $fileName;
}

class Member
{
    public $id = 0;
    public $firstname;
    public $lastname;
    public $email;
    public $password;
    public $exists;
    
    public function Member($id=NULL)
    {
        if($id == NULL)
            return;
        
        $this->id = $id;
        $this->FillMe($id);        
    }
    
    private function FillMe($id)
    {        
        $result = mysql_query("SELECT * FROM MEMBER WHERE ID=$id");
        
        if($result == null)
        {
            $exists = false;
            return;
        }
        
        $row = mysql_fetch_array($result);
        
        Member::FillObject($row,$this);
        
        $exists = true;
    }
    
    public static function FillObject($row,$member)
    {        
        $member->id = $row["ID"];
        $member->firstname = $row["firstname"];
        $member->lastname = $row["lastname"];
        $member->email = $row["email"];
        $member->password = $row["password"];        
    }
    
    public static function Login($email,$password)
    {
        $email = WebHelper::GetCleanString($email);
        $password = WebHelper::GetCleanString($password);
        
        $sql = "SELECT * FROM MEMBER WHERE EMAIL='$email' AND PASSWORD='$password'";
                
        $results = mysql_query($sql) or die("Can't get the member info");
                        
        if($row = mysql_fetch_array($results))
        {
            $_SESSION["MEMBER_ID"] = $row["ID"];
            //echo("SESSION = " . $_SESSION["MEMBER_ID"]);
            return true;
        }
    
        return false;          
    }
    
    public static function Logged()
    {        
        if($_SESSION["MEMBER_ID"] == NULL)
            return false;
        
        return true;
    }
    
    public static function GetCurrent()
    {
        if($_SESSION["MEMBER_ID"] != NULL)
        {
            return new Member($_SESSION["MEMBER_ID"]); 
        }
        
        return NULL;
    }
    
    public function Save()
    {
        //echo($this->name);
        
        if($this->id == 0)
        {
            $sql = "INSERT INTO MEMBER (FIRSTNAME,LASTNAME,EMAIL,PASSWORD) VALUES ('$this->firstname', '$this->lastname','$this->email','$this->password')";
        }
        else
        {
            $sql = "UPDATE MEMBER SET FIRSTNAME='$this->firstname', LASTNAME='$this->lastname', EMAIL='$this->email', PASSWORD='$this->password' WHERE ID=$this->id";            
        }
        
        //echo($sql);
        mysql_query($sql) or die("Can't Save the Place Object");       
    }
}


session_start();
            
?>